package com.haiyan.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.haiyan.base.BaseController;
import com.haiyan.user.bean.UserBean;
import com.haiyan.user.service.UserService;
import com.haiyan.user.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet(name="zhyUserController" , urlPatterns = "/zhyUser/post.do")
public class UserPostController extends BaseController {

    private UserService userService=new UserServiceImpl();
    private UserBean userBean = new UserBean();


    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String s = reader.readLine();

        JSONObject jsonObject = JSONObject.parseObject(s);
        String method = jsonObject.getString("method");
        String phone = jsonObject.getString("userPhone");
        String password = jsonObject.getString("userPassword");


        // 拿到method=什么就做什么事情
        if("findId".equals(method)){
            findUserIdByUserBean(phone,password,response);
        }


    }

    public void findUserIdByUserBean(String phone, String password,ServletResponse response){

        userBean.setPhone(phone);
        userBean.setPassword(password);

        UserBean userIdByUserBean = userService.findUserIdByUserBean(userBean);

        boolean check=userIdByUserBean != null;
        printData(check,userIdByUserBean,401,response);

    }
}
